package com.example.workoutjournal;

import java.util.List;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MyExercises extends Activity {
	public static final String SET_ID = "setName";
	private DAO database;
	
	
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		String toastText = "";
		super.onCreate(savedInstanceState);
		setTitle("My Exercises");
		getActionBar().hide();
		setContentView(R.layout.activity_my_exercises);
        TextView txt = (TextView) findViewById(R.id.mysets);
        Typeface font = Typeface.createFromAsset(getAssets(), "thin_skinned.ttf");
        txt.setTypeface(font);
		// Show the Up button in the action bar.
		
		database = new DAO(this);
		database.open();
		
		Intent intent = getIntent();
		toastText = intent.getStringExtra("TOAST_TEXT");
		if(toastText != null){
			Toast toast = Toast.makeText(this, toastText, Toast.LENGTH_SHORT);
	        toast.show();
		}
		final List<Set> values = database.getAllComments();
		ArrayAdapter<Set> adapter = new ArrayAdapter<Set>(this,android.R.layout.simple_list_item_1, values);
		ListView listview = (ListView) findViewById(R.id.list);
		listview.setAdapter(adapter);
		listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
	      @Override
	      public void onItemClick(AdapterView<?> parent, final View view,
	          int position, long id) {
	    	  Set getID = values.get(position);
	    	  Intent intent = new Intent(MyExercises.this, SetActivity.class);
	    	  intent.putExtra(SET_ID, getID.getID());
	    	  intent.putExtra("SET_NAME", getID.toString());
	    	  database.close();
	      	  startActivity(intent);
	      }
      });
		
		setupActionBar();
		
	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.my_exercises, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
    public void goToAddSet(View view){
    	Intent intent = new Intent(this, AddSet.class);
    	database.close();
    	startActivity(intent);
    }

}
